<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UserTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function saveUserData($users)
    {
		$password=md5($users['pwd']);
		$data = array(
			'user_name' 	  	       => $users['name'], 				
			'email' 		           => $users['email'],  		
			'password' 		           => $password,		
			'status'  	               => 0,  	
			'date_added' 	           => date('Y-m-d H:i:s'),   
			'date_updated'	  	       => date('Y-m-d H:i:s'), 	
			'user_type_id' 		       => $users['type_id'], 			
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;		
    }	
	public function checkDetails($details)
    {	
		$select = $this->tableGateway->getSql()->select();			
		$select->where('email="'.$details['email'].'"');
		$select->where('password="'.md5($details['pwd']).'"');
		$resultSet = $this->tableGateway->selectWith($select);	
		return $resultSet;		
	}
	public function getpassword($userid,$pwd){ 
		$pwd=md5($pwd);
		$select = $this->tableGateway->getSql()->select();
		$select->where('password="'.$pwd.'"');
		$select->where('user_id="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();			
		return $row;
	}
	public function userdetails($id)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->where('user_id="'.$id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();
	}
	public function checkUniqueRecord($email){        
		$select = $this->tableGateway->getSql()->select()                        
			   ->where('email="'.$email.'"');                                         
	   $resultSet = $this->tableGateway->selectWith($select);                        
	   $row=$resultSet->count();                
	   return $row;
   } 
	public function getMembersByMailId($email){        
		$select = $this->tableGateway->getSql()->select()                        
			   ->where('email="'.$email.'"');                                         
	   $resultSet = $this->tableGateway->selectWith($select);                
	   return $resultSet;
    }
	public function changepwd($userid,$pwd){
	   $password=md5($pwd);
	   $data = array(
		   'user_id'       =>$userid,
		   'password'      =>$password,
		);
	   $changepassword=$this->tableGateway->update($data, array('user_id' => $data['user_id']));	   
	   return $changepassword;                        
   }
   public function updateUserRegAuth($userid){
	   $data = array(
			'user_id'         =>$userid,
			'status'          =>'1',
	   );
	   $updateuserid=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
	   return $updateuserid;                        
	}
	public function updateUserData($users,$uid)
    {
		$password=md5($users['pwd']);
		$data = array(
			'user_name' 	  	       => $users['name'], 				
			'email' 		           => $users['email'],  		
			'password' 		           => $password,		
			'status'  	               => 1,  	
			'date_added' 	           => date('Y-m-d H:i:s'),   
			'date_updated'	  	       => date('Y-m-d H:i:s'), 	
			'user_type_id' 		       => $users['type_id'], 			
		);	
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $uid));
		return $updateuserid;	
    }
	public function getUserData($user_id){        
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('*'),'left');	
		$select->where('user.user_id="'.$user_id.'"');                                         
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
    }	
	public function getShopsByFilter($data)
	{
		$filters=array();
		if(isset($data['pincode'])){
		$filters['pincode']=$data['pincode'];
		}
		if(isset($data['location'])){
		$filters['location']=$data['location'];
		}
		if(isset($data['type_id'])){
		$filters['user_type_id']=$data['type_id'];
		}
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('*'),'left');	
		if(count($filters))
		{
			foreach($filters as $key=>$searchby)
			{
				if($searchby!='0')
				{
					if($key=='user_type_id')
					{
						$select->where('user.'.$key.'="'.$searchby.'"');
					}else{
						$select->where('user_details.'.$key.'="'.$searchby.'"');
					}
				}
			}
		}
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	public function changeUserStatus($status,$uid)
    {
		$password=md5($users['pwd']);
		$data = array(
			'status' 	  	       => $status['status'], 				
			'date_updated'	  	       => date('Y-m-d H:i:s'), 	
		);	
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $uid));
		return $updateuserid;	
    }
}